/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIAttribute;
import com.carnation.mong.platform.domain.MOIModel;
import com.carnation.mong.platform.domain.MOIPackage;
import com.carnation.mong.platform.service.ModelAttributeService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class ModelAttributeServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private ModelAttributeService service;

	public ModelAttributeServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(ModelAttributeService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getAttribute".equals(method)) {
			returnStr = getAttribute(item);
		} else if ("getAttributeInh".equals(method)) {
			returnStr = getAttributeInh(item);
		} else if ("getAttributes".equals(method)) {
			returnStr = getAttributes(item);
		} else if ("getAttributesInh".equals(method)) {
			returnStr = getAttributesInh(item);
		} else if ("getCountOfData".equals(method)) {
			returnStr = getCountOfData(item);
		} else if ("getModel".equals(method)) {
			returnStr = getModel(item);
		} else if ("getModelByPath".equals(method)) {
			returnStr = getModelByPath(item);
		} else if ("getModels".equals(method)) {
			returnStr = getModels(item);
		} else if ("getMPackage".equals(method)) {
			returnStr = getMPackage(item);
		} else if ("getPath".equals(method)) {
			returnStr = getPath(item);
		} else if ("hasData".equals(method)) {
			returnStr = hasData(item);
		} else if ("isInheritedFrom".equals(method)) {
			returnStr = isInheritedFrom(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 2) {// 2个参数
			MOIModel instance = instanceMOIModel(item.getChildren().get(0));
			List<MOIAttribute> attrList = instanceMOIAttributeList(item
					.getChildren().get(1));
			if (log.isDebugEnabled()) {
				log.debug("MOIModel:" + instance + ",attrList:" + attrList);
			}
			Object obj = null;
			try {
				obj = service.create(instance, attrList);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			MOIModel instance = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("delete MOIModel:" + instance);
			}
			Object obj = null;
			try {
				service.delete(instance);
				obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getAttribute(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			String id = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getAttribute id:" + id);
			}
			Object obj = null;
			try {
				obj = service.getAttribute(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 2) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			String attrName = getString(item.getChildren().get(1)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getAttribute m:" + m + ",attrName:" + attrName);
			}
			Object obj = null;
			try {
				obj = service.getAttribute(m, attrName);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getAttributeInh(Item item) {
		if (item.getChildren().size() == 2) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			String attrName = getString(item.getChildren().get(1)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getAttributeInh m:" + m + ",attrName:" + attrName);
			}
			Object obj = null;
			try {
				obj = service.getAttributeInh(m, attrName);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getAttributes(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getAttributes m:" + m);
			}
			Object obj = null;
			try {
				obj = service.getAttributes(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getAttributesInh(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getAttributesInh m:" + m);
			}
			Object obj = null;
			try {
				obj = service.getAttributesInh(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getCountOfData(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getCountOfData m:" + m);
			}
			Object obj = null;
			try {
				obj = service.getCountOfData(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getModel(Item item) {
		if (item.getChildren().size() == 1) {// 个参数
			if (item.getChildren().get(0).getLabel().equals(
					MOIAttribute.class.getSimpleName())) {
				MOIAttribute attr = instanceMOIAttribute(item.getChildren()
						.get(0));
				if (log.isDebugEnabled()) {
					log.debug("getModel attr:" + attr);
				}
				try {
					Object obj = service.getModel(attr);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			} else {
				String id = getString(item.getChildren().get(0).getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("getModel id:" + id);
				}
				try {
					Object obj = service.getAttribute(id);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		} else if (item.getChildren().size() == 2) {
			if (item.getChildren().get(0).getLabel().equals(
					MOIPackage.class.getSimpleName())) {
				MOIPackage m = instanceMOIPackage(item.getChildren().get(0));
				String attrName = getString(item.getChildren().get(1)
						.getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("getAttribute m:" + m + ",attrName:" + attrName);
				}
				Object obj = null;
				try {
					obj = service.getModel(m, attrName);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					return e.toString();
				}
			} else {
				String packagePath = getString(item.getChildren().get(0)
						.getLabelParts()[1]);
				String name = getString(item.getChildren().get(1)
						.getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("getAttribute packagePath:" + packagePath
							+ ",attrName:" + name);
				}
				try {
					Object obj = service.getModel(packagePath, name);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					return e.toString();
				}
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getModelByPath(Item item) {
		if (item.getChildren().size() == 1) {
			String path = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getModelByPath path:" + path);
			}
			try {
				Object obj = service.getModelByPath(path);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getModels(Item item) {
		if (item.getChildren().size() == 1) {
			MOIPackage m = instanceMOIPackage(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getModels p" + m);
			}
			Object obj = null;
			try {
				obj = service.getModels(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getMPackage(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getMPackage m:" + m);
			}
			Object obj = null;
			try {
				obj = service.getMPackage(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getPath(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("getPath m:" + m);
			}
			Object obj = null;
			try {
				obj = service.getPath(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String hasData(Item item) {
		if (item.getChildren().size() == 1) {
			MOIModel m = instanceMOIModel(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("hasData m:" + m);
			}
			Object obj = null;
			try {
				obj = service.hasData(m);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String isInheritedFrom(Item item) {
		if (item.getChildren().size() == 2) {
			MOIModel childModel = instanceMOIModel(item.getChildren().get(0));
			MOIModel parentModel = instanceMOIModel(item.getChildren().get(1));
			if (log.isDebugEnabled()) {
				log.debug("isInheritedFrom childModel:" + childModel
						+ ",parentModel:" + parentModel);
			}
			try {
				Object obj = service.isInheritedFrom(childModel, parentModel);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String update(Item item) {
		if (item.getChildren().size() == 2) {// 个参数
			MOIModel instance = instanceMOIModel(item.getChildren().get(0));
			List<MOIAttribute> attrList = instanceMOIAttributeList(item
					.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("update m:" + instance + ",attrList:" + attrList);
			}
			Object obj = null;
			try {
				service.update(instance, attrList);
				obj = service.getModel(instance.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
